package apiSpeech

import (
	"baidu/AipBase"
	"baidu/client"
	"baidu/params"
	"reflect"
	"xiaoYuShop/go/front/tool/md5"
)

/**
 * Api 请求
 * @param  string $url
 * @param  mixed $data
 * @return mixed
 */
func Request(url string, data map[string]interface{}) string {
	var paramsR map[string]interface{} = map[string]interface{}{}

	var authObj map[string]interface{} = AipBase.Auth(false, params.AccessTokenUrl, map[string]interface{}{
		"grant_type":    "client_credentials",
		"client_id":     params.ApiKey,
		"client_secret": params.SecretKey,
	})

	if params.IsCloudUser == false {
		paramsR["access_token"] = authObj["access_token"]
	}

	data["aipSdk"] = "php"
	data["cuid"] = md5.ToMd5(reflect.ValueOf(paramsR["access_token"]).String())
	data["aipSdkVersion"] = params.Version
	data["tok"] = paramsR["access_token"]

	content := client.Post(url, data, nil)

	return reflect.ValueOf(content).String()
}

func Synthesis(text string, lang string, ctp string, options map[string]interface{}) string {
	var data map[string]interface{} = map[string]interface{}{}

	data["tex"] = text
	data["lan"] = lang
	data["ctp"] = ctp

	for k, item := range options {
		data[k] = item
	}

	return Request(params.TtsUrl, data)
}

/**
 * 处理请求参数
 * @param  string $url
 * @param array $params
 * @param array $data
 * @param array $headers
 */
func ProcessRequest(paramsR map[string]interface{}) map[string]interface{} {
	paramsR["aipSdk"] = "php"
	paramsR["aipSdkVersion"] = params.Version
	return paramsR
}
